//
//  JumpMapTools.m
//  CZT_HN_Longrise
//
//  Created by 程虎 on 2017/8/30.
//  Copyright © 2017年 OSch. All rights reserved.
//

#import "LocationTools.h"

@interface LocationTools()<UIActionSheetDelegate>
@property (nonatomic, assign) BOOL isNext;
@end

@implementation LocationTools

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.isNext = YES;
    }
    return self;
}

#pragma mark - 开始定位
- (void)startLocation
{
    //判断定位功能是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        self.manager = [[CLLocationManager alloc]init];
        self.manager.delegate = self;
        [self.manager requestAlwaysAuthorization];
        [self.manager requestWhenInUseAuthorization];
        
        //设置寻址精度
        self.manager.desiredAccuracy = kCLLocationAccuracyBest;
        self.manager.distanceFilter = 5.0;
        [self.manager startUpdatingLocation];
    }
}

#pragma mark CoreLocation delegate (定位失败)
//定位失败后调用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if (self.isNext) {
        if (self.locationBlock) {
            self.locationBlock(nil);
        }
    }
}

#pragma mark 定位成功后则执行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    [self.manager stopUpdatingLocation];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //旧址
        CLLocation *currentLocation = [locations lastObject];
        CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
        //打印当前的经度与纬度
        NSLog(@"latitude --- %f,longitude --- %f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
        
        
        __weak typeof(&*self) weakSelf = self;
        //反地理编码
        [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (placemarks.count > 0) {
                CLPlacemark *placeMark = placemarks[0];
                
                /*看需求定义一个全局变量来接收赋值*/
                //            NSLog(@"当前国家 --- %@",placeMark.country);//当前国家
                //            NSLog(@"%@",placeMark.locality);//当前的城市
                //            NSLog(@"%@",placeMark.subLocality);//当前的位置
                //            NSLog(@"%@",placeMark.thoroughfare);//当前街道
                //            NSLog(@"%@",placeMark.name);//具体地址
                
                NSArray *arys = placeMark.addressDictionary[@"FormattedAddressLines"];
                NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:placeMark.addressDictionary];
                [dict setValue:[NSString stringWithFormat:@"%f",placeMark.location.coordinate.latitude] forKey:@"latitude"];
                [dict setValue:[NSString stringWithFormat:@"%f",placeMark.location.coordinate.longitude] forKey:@"longitude"];
                [dict setValue:[[arys firstObject] substringFromIndex:2] forKey:@"address"];
                
                LocationModel *model = [LocationModel mj_objectWithKeyValues:dict];
                if (self.isNext) {
                    self.isNext = NO;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (weakSelf.locationBlock) {
                            weakSelf.locationBlock(model);
                        }
                    });
                }
            }
            else {
                
            }
        }];
    });
}

@end
